<?php
namespace Shopmobile\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $user;
	public $usernick;
	public function  __construct (){
		parent::__construct();
		if(C("TAE")){
			Vendor("TopClient");
		}else{
			Vendor("taobaosdk.top.TopClient");
		}
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$sid = I("sid");
		if($sid){
			cookie("sid",$sid);
			$this->sid = $sid;
			//cookie保存sid，读取不到则跳出
		}else{
			$this->sid = cookie("sid");
		}
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Index:not_allow");
			die();
		}
		$this->mixednick = I("tida_mixnick");
		if(C("TAE")){
			$this->mixednick = urldecode_utf8($this->mixednick);
		}else{
			$this->mixednick = urldecode($this->mixednick);
		}
		$usernick = I("usernick");
		if(C("TAE")){
			$this->usernick = urldecode_utf8($usernick);
		}else{
			$this->usernick = urldecode($usernick);
		}
		$this->shop_uid = I("shop_uid");
		if(!$this->shop_uid && $this->usernick){
			//先读取保存的混淆昵称
			$MIX = M("UserMixnick");
			$this->mixednick = $MIX->where(Array("usernick"=>$this->usernick))->getField("mixednick");
			if(!$this->mixednick){
				if(C("TAE")){
					$user_resp = json_decode($fetchService->fetchByGet(C("EWS")."?m=service&c=mixnick&a=get_mixnick&nick=".$usernick));
				}else{
					Vendor("taobaosdk.top.request.MixnickGetRequest");
					$c = new \TopClient;
					$req = new \MixnickGetRequest;
					$req->setNick($this->usernick);
					$c->appkey = C("FTM_KEY");
					$c->secretKey = C("FTM_SECRET");
					$user_resp = $c->execute($req);
				}
				// print_r($user_resp);
				$this->mixednick=(string)$user_resp->nick;
				if(C("TAE")){
					$this->mixednick = urldecode_utf8($this->mixednick);
				}else{
					$this->mixednick = urldecode($this->mixednick);
				}
				if(!$this->mixednick){
					$this->assign("msg","读取用户昵称错误！");
					$this->display("Index:not_allow");
					die();
				}
				$MIX->add(Array("usernick"=>$this->usernick,"mixednick"=>$this->mixednick));
			}
		}
		if($this->shop_uid && !$this->mixednick){
			$USR = D("UserView");
			$USR->sid=$this->sid;
			$USR->uid=$this->shop_uid;
			$USR->myfields="sys_uid,mixednick";
			$re = $USR->getUserField();
			$this->mixednick = current($re);
			$this->sys_uid=key($re);
		}
		if(!$this->shop_uid && $this->mixednick){
			// $this->user = S($this->mixednick);
			if(!$this->user){
				$USR = D("UserView");
				$USR->mixednick = $this->mixednick;
				$USR->sid = $this->sid;
				$USR->getUser();
				if(!$USR->user_info["shop_uid"]){
					$USR->usernick = $this->usernick;
					$USR->addUser();
				}
				//
				// $USR->user_info["sql"]=$USR->_sql();
				// $USR->user_info["mixednick"]=$this->mixednick;
				//
				$this->user = $USR->user_info;
				$this->sys_uid = $USR->user_info["sys_uid"];
				$this->shop_uid = $USR->user_info["shop_uid"];
				S($this->mixednick,$this->user);
			}		
			$friend = I("friend");
			$FIR = D("Friendships");
			$FIR->sid = $this->sid;
			$FIR->home_id = $this->shop_uid;
			$FIR->home_mixednick = $this->mixednick;
			// $codeimg = ($FIR->get_code_img()); //邀请二维码
			if($friend){
				$FIR->home_id=null;
				$FIR->guest_id = $this->shop_uid;
				$FIR->home_mixednick = $friend;
				$FIR->guest_mixednick = $this->mixednick;
				$FIR->invite(); //邀请逻辑
			}	
		}
		
		$this->assign("appkey",C("FT_KEY"));
		$this->assign("appname",cookie("appname"));
		$this->assign("codeimg",$codeimg);
		$this->assign("sid",$this->sid);
		$this->assign("user",$this->user);
	}
	public function ajaxReturn($reda){ 
		$reda = json_encode($reda);
		$re = I("get.callback")?I("get.callback").'('.$reda.')':$reda;
		header("KissyIoDataType:json");		
		header("Access-Control-Allow-Origin: ".C("Allow_Origin"));
		echo $re;
	}
}